import produce from "immer";

const initState = {
    size: localStorage.getItem('size') || 'middle',
    lang: localStorage.getItem('lang') || navigator.language.split('-')[0], // zh_CN
    color: localStorage.getItem('color') || '#1890ff'
}

// 一个子仓库就是一个子reducer
// 接收2参数 state action
// reducer是纯函数 不能直接修改入参，相同的入参永远得到相同的结果
function reducer(state = initState, { type, payload }) {
    // 固定流程
    // 1. 收到type，深拷贝state
    // 2. 根据type和payload对state进行操作
    // 3. 返回修改后最新的state

    // const newState=produce(oldState,(newState)=>{操作state --- CRUD})
    return produce(state, (newState) => {
        switch (type) {
            case 'APP_SIZE':
                newState.size = payload
                localStorage.setItem('size', payload)
                break;
            case 'APP_LANG':
                newState.lang = payload
                localStorage.setItem('lang', payload)
                break;
            case 'APP_COLOR':
                newState.color = payload
                localStorage.setItem('color', payload)
                break;
        }
    })
}

export default reducer